"""
@File    :  common_function.py
@Author  :  LeeSen
@Date    :  2024-06-28
@Version :  Python 3.7.4
"""
import ipaddress
import re
from datetime import datetime
from scapy.arch import get_ips
from scapy.interfaces import ifaces


def ip_format_check(ip_address):
    try:
        ipaddress.IPv4Address(ip_address)
        return False
    except Exception as a:
        return True


def text_input_check(text_string):
    if " " in text_string or len(text_string) == 0:
        return True
    return False


def int_num_check(number):
    try:
        num = int(number)
        return False
    except ValueError as e:
        return True


def validate_mac(value):
    if value.find('-') != -1:
        pattern = re.compile(r"^\s*([0-9a-fA-F]{2,2}-){5,5}[0-9a-fA-F]{2,2}\s*$")
        if pattern.match(value):
            return True
        else:
            return False
    if value.find(':') != -1:
        pattern = re.compile(r"^\s*([0-9a-fA-F]{2,2}:){5,5}[0-9a-fA-F]{2,2}\s*$")
        if pattern.match(value):
            return True
        else:
            return False


def welcome_init(log_area):
    log_area.append(".____     ______________________ ____________________ _______    ")
    log_area.append("|    |    \_   _____/\_   _____//   _____/\_   _____/ \      \   ")
    log_area.append("|    |     |    __)_  |    __)_ \_____  \  |    __)_  /   |   \  ")
    log_area.append("|    |___  |        \ |        \/        \ |        \/    |    \ ")
    log_area.append("|_______ \/_______  //_______  /_______  //_______  /\____|__  / ")
    log_area.append("        \/        \/         \/        \/         \/         \/  ")


def init_nic(nic_config_box):
    ifaces.reload()
    nic_config_box.clear()
    iface = get_ips()
    for item in iface.keys():
        #if "OK" in (item.flags.__str__()).split("+") and not str(item.ip) is "":
        if not str(item.ip) is "":
            nic_config_box.addItem(item.description)


def clear_log(log_area):
    log_area.clear()


def clear_table_data(table):
    row_cnt = table.rowCount()
    while row_cnt != -1:
        table.removeRow(row_cnt)
        row_cnt -= 1


def log_format_print(log_area, message):
    log_area.append("{} - {}".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), message))


def debug_log_format_print(message):
    print("{} - {}".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), message))
